﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace LenderClearingHouse
{
    public partial class MoveFileDialog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            string[] split = CurrentFoldersPath.Split('\\');
            string rootFolder = split.Length > 0 ? split[0] : "Property";

            if (split.Length > 0)
            {
                RadTreeNode node = new RadTreeNode(split[0]);
                tree.Nodes.Add(node);
                AddSubFolders(node.Nodes, Path.Combine(PropertyPhysicalDir, split[0]));
            }
            else
            {
                AddSubFolders(tree.Nodes, PropertyPhysicalDir);
            }

            tree.ExpandAllNodes();
        }

        void AddSubFolders(RadTreeNodeCollection rootNode, string physicalPath)
        {
            foreach (DirectoryInfo di in new DirectoryInfo(physicalPath).GetDirectories())
            {
                RadTreeNode node = new RadTreeNode(di.Name);
                rootNode.Add(node);

                AddSubFolders(node.Nodes, Path.Combine(physicalPath, di.Name));
            }
        }

        string PropertyPhysicalDir
        {
            get { return Session["PropertyPhysicalDir"].ToString(); }
            set { Session["PropertyPhysicalDir"] = value; }
        }

        string CurrentFoldersPath
        {
            get
            {
                var result = Session["CurrentFoldersPath"] as string;
                if (result == null)
                {
                    result = "";
                    Session["CurrentFoldersPath"] = result;
                }
                return result;
            }
        }

        protected void btnMove_Click(object sender, EventArgs e)
        {
            try
            {
                string curFileName = Path.Combine(
                    Path.Combine(PropertyPhysicalDir, CurrentFoldersPath),
                    Request.QueryString["fileName"].ToString());
                string newFileName = Path.Combine(
                    Path.Combine(PropertyPhysicalDir, tree.SelectedNode.FullPath),
                    Request.QueryString["fileName"].ToString());

                if (File.Exists(newFileName))
                {
                    throw new NotSupportedException();
                }
                
                File.Move(curFileName, newFileName);

                ScriptManager.RegisterStartupScript(this, this.GetType(),
                    "key", string.Format("CloseAndRefresh();"), true);
            }
            catch (NotSupportedException ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(),
                    "key", string.Format("alert('File already exists in destination folder');"), true);
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(),
                    "key", string.Format("alert('Error moving file');"), true);
            }
        }
    }
}